/* ***** BEGIN LICENSE BLOCK *****
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Netscape Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/NPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is lutsko.com code.
 *
 * The Initial Developer of the Original Code is  James Lutsko 
 * (jim@lutsko.com http://www.lutsko.com).
 * Portions created by the Initial Developer are Copyright (C) 2003
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the NPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the NPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

package autoschedule;

import java.io.Serializable;
import java.util.Vector;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Collections;

public class Team implements Serializable
{
    int num;
    Vector games = null;
    League league = null;
    transient int totalGames = 0;
    String name = null;
    boolean _isInterLeague = false;

    public Team(int _num, League l)
    {
	num = _num;
	league = l;
	games = new Vector();
    }
    
    void addGame(Game g){games.add(g);sortGames();}

    public String toString(){ return (name == null ? league.getName() + Integer.toString(num+1): name);}

    public boolean isInterLeague(){return _isInterLeague;}

    public League getLeague(){return league;}
    public int getNumberOfGames(){return (games == null ? 0 : games.size());}

    public Game getGame(int r){return (games == null ? null : (Game) games.get(r));}

    Game lastGame()
    {
	if(games == null || games.size() == 0) return null;
	return (Game) games.lastElement();
    }

    double gamesPerWeek()
    {
	if(games == null || games.size() < 2) return 1;

	Calendar c = Calendar.getInstance();
	int currentWeek = -1;	
	double weeks = 0;
	double ngames = 0.0;
	for(int i=0;i<games.size();i++)
	    {
		Game g = (Game) games.get(i);
		if(g.getDate() == null) continue;
		ngames++;

		int theWeek = g.getWeekOfYear();
		if(theWeek != currentWeek)
		    {
			weeks++;
			currentWeek = theWeek;
		    }
	    }
	return (weeks <= 0 ? 0 : ngames/weeks); //games/week

    }

    public void sortGames() 
    {
	Collections.sort(games, new Comparator() {
		public int compare(Object o1, Object o2) {
		    if(o1 == o2) return 0;
		    Game g1 = (Game) o1;
		    Game g2 = (Game) o2;
		    if(g2 == null) return 1;
		    else if(g1 == null) return -1;
		    
		    int d1 = g1.getDayOfYear();
		    int d2 = g2.getDayOfYear();
		    
		    int ret = (d1 < d2 ? -1 : (d1 == d2 ? 0 : 1)); 
		    if(ret == 0 && g1.time != null && g2.time != null)
			ret = g1.time.compareTo(g2.time);
		    if(ret == 0 && g1.time == null && g2.time != null) return 1;
		    if(ret == 0 && g1.time != null && g2.time == null) return -1;
		    if(ret == 0)
			{
			    if(g1.getFacilityName() == g2.getFacilityName()) return 0;
			    if(g1.getFacilityName() == null) return -1;
			    if(g2.getFacilityName() == null) return 1;
			    ret = g1.getFacilityName().compareTo(g2.getFacilityName());
			}

		    return ret;
		}});
    }


    static final long serialVersionUID = 1768359049715607511L;
}
